package com.example.demo.common;


import com.example.demo.enums.OrderByEnum;
import org.apache.commons.lang3.StringUtils;

public class QueryBaseParam {


    /**默认页数**/
    public static final int DEFAULT_PAGE = 1;

    /** 默认每页显示的记录条数： {@value} */
    public static final int DEFAULT_PAGESIZE = 100;

    public static final int MAX_PAGESIZE = 100;

    /** 页码，从 1 开始 */
    public int pageNumber = 1;

    /** 每页显示的记录条数，默认值为 {@link #DEFAULT_PAGESIZE} */
    public int pageSize = DEFAULT_PAGESIZE;

    /**偏移量**/
    private int offset = 0;


    /**排序值**/
    private String orderName = "";


    /**排序方式 com.example.demo.enums.OrderByEnum**/
    private String orderBy = "";


    public QueryBaseParam() {
    }

    public QueryBaseParam(int pageNumber, int pageSize) {
        correctPage(pageNumber,pageSize);
    }


    public QueryBaseParam(int pageNumber, int pageSize, String orderName) {
        correctPage(pageNumber,pageSize);
        this.orderName = orderName;
        if (StringUtils.isNotEmpty(orderName)){
            this.orderBy = OrderByEnum.ASC.getOrderBy();
        }
    }

    public QueryBaseParam(int pageNumber, int pageSize, String orderName, String orderBy) {
        this.pageNumber = pageNumber;
        this.pageSize = pageSize;
        this.orderName = orderName;
        this.orderBy = orderBy;
        this.offset = getOffset();
    }

    public QueryBaseParam(String orderName, String orderBy) {
        this.orderName = orderName;
        this.orderBy = orderBy;
        if (StringUtils.isEmpty(orderName)){
            this.orderBy = "";
        }
    }

    public void correctPage(int pageNumber, int pageSize){
        if (pageNumber < 1){
            pageNumber = DEFAULT_PAGE;
        }
        if (pageSize < 0 || pageSize > MAX_PAGESIZE){
            pageSize = MAX_PAGESIZE;
        }
        this.pageNumber = pageNumber;
        this.pageSize = pageSize;
        this.offset = getOffset();
    }

    /**
     * 获取当前页的偏移量
     *
     * @return 偏移量
     */
    public int getOffset() {
        return (getPageNumber() - 1) * pageSize;
    }




    public int getPageNumber() {
        return pageNumber;
    }

    public void setPageNumber(int pageNumber) {
        this.pageNumber = pageNumber;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public String getOrderName() {
        return orderName;
    }

    public void setOrderName(String orderName) {
        this.orderName = orderName;
    }

    public String getOrderBy() {
        return orderBy;
    }

    public void setOrderBy(String orderBy) {
        this.orderBy = orderBy;
    }


}
